package com.dmgis.qxfw.dataservice.businessservice.yydl.service.Impl;

import com.dmgis.entity.DmMapPoint;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.RequestDataBySelect;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.msgmediumsmallscale;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.msgmediumsmallscale;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.MsgMedium;
import com.dmgis.qxfw.dataservice.businessservice.common.util.JsonUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.StringUtil;
import com.dmgis.qxfw.dataservice.businessservice.controller.ProcessRequestController;
import com.dmgis.qxfw.dataservice.businessservice.yydl.service.WeatherMonitorServiceI;
import com.dmgis.qxfw.dataservice.gisservice.service.ITransformService;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class WeatherMonitorServiceImpl implements WeatherMonitorServiceI {

	ProcessRequestController pc = new ProcessRequestController();

	@Resource
	private ITransformService transformService;
	@Override
	public Date initMonitorTime() {
		Date date = new Date();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		String[] columns = new String[] { "to_char(observtime, 'yyyy-MM-dd HH24:MI:SS') as observtime" };
		selectRequest.setColumns(columns);
		selectRequest.setSort(new String[] { "observtime desc" });
		selectRequest.setLimit(1);
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		strWhere.append(String.format("and (maxtemp>='%d'", 30));
		strWhere.append(String.format("or mintemp<='%d')", 8));
		strWhere.append(String.format("and windvelocity>='%d'", 0));
		strWhere.append(String.format("and rain>='%d'", 0));
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "msgmediumsmallscale");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<msgmediumsmallscale>>() {
			}).get(0).getObservtime();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return date;
	}

	@Override
	public List<MsgMedium> getMsgMedium(String strStartTime, String strEndTime, String maxTemp, String minTemp,
			String windVal, String rainVal, boolean bMainTable) {
		List<msgmediumsmallscale> listData = new ArrayList<msgmediumsmallscale>();
		List<MsgMedium> resultList = new ArrayList<MsgMedium>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("stationid");
		columns.add("StationName");
		columns.add("to_char(observTime, 'yyyy-MM-dd HH24:MI:SS') as observTime");
		columns.add("maxtemp");
		columns.add("mintemp");
		columns.add("windvelocity");
		columns.add("rain");
		columns.add("longitude");
		columns.add("latitude");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		selectRequest.setSort(new String[] { "observTime desc" });
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (strStartTime != null) {
			strWhere.append(String.format(" and observTime>='%s'", strStartTime));
		}
		if (strEndTime != null) {
			strWhere.append(String.format(" and observTime<='%s'", strEndTime));
		}
		if (maxTemp != null) {
			strWhere.append(String.format(" and (maxtemp>='%d'", Integer.parseInt(maxTemp)));
		}
		if (minTemp != null) {
			strWhere.append(String.format(" or mintemp<='%d')", Integer.parseInt(minTemp)));
		}
		if (windVal != null) {
			strWhere.append(String.format(" and windvelocity>='%d'", Integer.parseInt(windVal)));
		}
		if (rainVal != null) {
			strWhere.append(String.format(" and rain>='%d'", Integer.parseInt(rainVal)));
		}
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		strWhere.append(" and city like '"+DbConfig.CITY+"%' ");
		selectRequest.setParam(strWhere.toString());
		if (bMainTable) {
			try {

				selectParam.put("method", "msgmediumsmallscale");
				selectParam.put("sqlType", "select");
				selectParam.put("param", JsonUtil.object2Json(selectRequest));
				String message = pc.processRequest(selectParam);
				listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<msgmediumsmallscale>>() {
				});
			} catch (Exception e) {
				// TODO: handle exception
			}
			if (listData != null && listData.size() > 0) {
				for (msgmediumsmallscale msg : listData) {
					MsgMedium smallscale = new MsgMedium();
					smallscale.setObservTime(msg.getObservtime());
					smallscale.setStationID(msg.getStationid());
					smallscale.setStationName(msg.getStationname());
					smallscale.setMyMaxTemp(msg.getMaxtemp());
					smallscale.setMyMinTemp(msg.getMintemp());
					smallscale.setWindVelocity(msg.getWindvelocity());
					smallscale.setRain(msg.getRain());
					smallscale.setLongitude(msg.getLongitude().floatValue());
					smallscale.setLatitude(msg.getLatitude().floatValue());
					DmMapPoint dmDot = new DmMapPoint(msg.getLongitude(), msg.getLatitude());
					DmMapPoint ltDot = transformService.LonLatConvertToLambert(dmDot.x, dmDot.y);
					smallscale.setY(ltDot.y);
					smallscale.setX(ltDot.x);
					resultList.add(smallscale);
				}
			}
		} else {
			try {
				selectParam.put("method", "msgmediumsmallscale");
				selectParam.put("sqlType", "select");
				selectParam.put("param", JsonUtil.object2Json(selectRequest));
				String message = pc.processRequest(selectParam);
				listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<msgmediumsmallscale>>() {
				});
			} catch (Exception e) {
				// TODO: handle exception
			}
			if (listData != null && listData.size() > 0) {
				for (msgmediumsmallscale msg : listData) {
					MsgMedium smallscale = new MsgMedium();
					smallscale.setObservTime(msg.getObservtime());
					smallscale.setStationID(msg.getStationid());
					smallscale.setStationName(msg.getStationname());
					smallscale.setMyMaxTemp(msg.getMaxtemp());
					smallscale.setMyMinTemp(msg.getMintemp());
					smallscale.setWindVelocity(msg.getWindvelocity());
					smallscale.setRain(msg.getRain());
					smallscale.setLongitude(msg.getLongitude().floatValue());
					smallscale.setLatitude(msg.getLatitude().floatValue());
					DmMapPoint dmDot = new DmMapPoint(msg.getLongitude(), msg.getLatitude());
					DmMapPoint ltDot = transformService.LonLatConvertToLambert(dmDot.x, dmDot.y);
					smallscale.setY(ltDot.y);
					smallscale.setX(ltDot.x);
					resultList.add(smallscale);
				}
			}
		}

		return resultList;
	}

}
